package com.dmgis.qxfw.dataservice.businessservice.ctstour.controller;

import com.dmgis.entity.DmMapPoint;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.tour_fcst;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.tour_smallscale;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.ForecastKey;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.TourFcstInfo;
import com.dmgis.qxfw.dataservice.businessservice.common.util.DateTime;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JinUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.WebCtrlData;
import com.dmgis.qxfw.dataservice.businessservice.ctstour.service.Impl.CTSTourInfoService;
import com.dmgis.qxfw.dataservice.gisservice.service.ITransformService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.dsig.TransformService;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ctstour")
public class CTSTourController {

    @Resource
    CTSTourInfoService ctstourinfo;


    @Resource
    ITransformService transformService;

    /**
     * 获取城头山旅游景区预警
     *
     * @param request
     * @param model
     * @return
     * @throws Exception
     */

    @RequestMapping("/getCTSHourTourFcst")
    @ResponseBody
    public Object getTourFcst(HttpServletRequest request, Model model) throws Exception {

        ForecastKey selectKey = new ForecastKey();// 查询类
        String startDate = request.getParameter("startDate") + ":00:00";// 时间
        String endDate = request.getParameter("endDate") + ":00:00";// 时次
        String strStationID = "";
        String strCountyName = "";
        int nTimes;
        List<TourFcstInfo> resultList = new ArrayList<TourFcstInfo>();
        WebCtrlData wcd = new WebCtrlData();
        try {
            selectKey.setStationID("XT0002");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            selectKey.setDateChar(sdf.parse(startDate));
            selectKey.setEnddateChar(sdf.parse(endDate));
            List<tour_fcst> fcstList = ctstourinfo.getTourFcstInfo(selectKey);

            int count = fcstList.toArray().length;
            for (int i = 0; i < count; i++) {
                strStationID = fcstList.get(i).getStationid();
                strCountyName = fcstList.get(i).getStationname();
                String windD = "";// 风向信息
                String windV = "";// 风速信息
                String windD12 = wcd.GetAllFx(fcstList.get(i).getWindd().toString().replace("-65535", "0").trim());
                String windV12 = wcd.GetWeatherFs(String
                        .valueOf(WebCtrlData.FengSuToFengJi(Double.parseDouble(fcstList.get(i).getWinds()))));
                windD += windD12;
                windV += windV12;
                double maxTemp = fcstList.get(i).getMaxtemp();
                double minTemp = fcstList.get(i).getMintemp();
                String pic1 = String.format("%02d",
                        Integer.parseInt(WebCtrlData.WeatherNameToPicPath(fcstList.get(i).getWeather1())));
                TourFcstInfo fcstInfo = new TourFcstInfo();
                fcstInfo.setStationId(strStationID);//站定ID
                fcstInfo.setStationName(strCountyName);//站点名称
                fcstInfo.setMinTemp(minTemp);//最低气温
                fcstInfo.setMaxTemp(maxTemp);//最高气温
                fcstInfo.setRain(fcstList.get(i).getRain());//降水量
                fcstInfo.setWindD(windD);//风向
                fcstInfo.setWindV(windV);//风速
                fcstInfo.setPic1(pic1);
                fcstInfo.setLat(fcstList.get(i).getLatitude());//维度
                fcstInfo.setLon(fcstList.get(i).getLongitude());//经度
                DmMapPoint dmDot = new DmMapPoint(fcstList.get(i).getLongitude(), fcstList.get(i).getLatitude());
                DmMapPoint ltDot = transformService.LonLatConvertToLambert(dmDot.x, dmDot.y);
                fcstInfo.setY(ltDot.y);
                fcstInfo.setX(ltDot.x);
                fcstInfo.setWeather1(fcstList.get(i).getWeather1().trim());
                String weather = fcstList.get(i).getWeather1().trim();
                fcstInfo.setWeather(weather);
                fcstInfo.setHumid(String.valueOf(fcstList.get(i).getHumid()) + "%");
                fcstInfo.setTime(new DateTime(fcstList.get(i).getDatechar()).toDateTimeString());
                resultList.add(fcstInfo);
            }
        } catch (Exception e) {
            // TODO: 查询异常
            e.printStackTrace();
        }
        return resultList;
    }

    /**
     * 区县选择，获得（整点）雨量、温度、湿度、气压、风速
     *
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/getPointSK")
    @ResponseBody
    public Object getPointSK(HttpServletRequest request, Model model) throws Exception {
        List<tour_smallscale> resultList = ctstourinfo.getPointSK();
        return resultList;
    }


    /**
     * 指数预报展示
     *
     * @param request
     * @param model
     * @return
     * @author dsq
     * @date 2018年8月23日
     */
    @RequestMapping("/showZhiShuInfo")
    @ResponseBody
    public Object showZhiShuInfo(HttpServletRequest request, Model model) {
        String result = "";
        Map<String, String> zs = DbConfig.getZhiShuType();
        ForecastKey selectKey = new ForecastKey();// 查询类
        String time = request.getParameter("date");// 时间
        String sc = request.getParameter("sc");// 时次
        String county = request.getParameter("county");// 区县
        String tourname = request.getParameter("tourname");// 景区
        String index = request.getParameter("index");// 指数
        try {
            selectKey.setTimeChar(sc);
            selectKey.setDateChar(DateTime.parseDate(time).toDate());
            selectKey.setCounty(county);
            selectKey.setTourname(tourname);
            List<Map<String, Object>> resultList = ctstourinfo.getZhiShuInfo(selectKey);
            String dtime = new DateTime(selectKey.getDateChar()).toDateTimeString("yyyy年MM月dd日") + sc + "指数预报";
            result = "<div class='textinfo'><table class='ybcontent'>";
            StringBuilder cont = new StringBuilder();
            for (Map<String, Object> map : resultList) {
                Iterator<Map.Entry<String, String>> iterator = zs.entrySet().iterator();
                Map.Entry<String, String> entry;
                while (iterator.hasNext()) {
                    entry = iterator.next();
                    String zhishu = entry.getValue();
                    String key = entry.getKey();
                    String level = map.get(key + "grade").toString();
                    String note = map.get(key + "text").toString();
                    cont.append("<tr valign='top'>");
                    cont.append(
                            "<td width='20%'><div class='zhishurow'><div class='zhishuimg' style='float:left'><img src='images/zhishu/"
                                    + zhishu + ".png' alt='' style='width: 30px;'/></div>");
                    cont.append("<div class='zhishucont' style='font-size:12px'><div class='zhishulevel'>" + zhishu + "：" + level
                            + "</div><div class='zhishunote'>" + note + "</div></div></div></td></tr>");
                }
            }
            result += cont.toString() + "</table></div>";
        } catch (Exception e) {
            // TODO: handle exception
        }
        return result;
    }
}
